﻿using FictionsDownload.BLL;
using FictionsDownload.Model;
using FictionsDownload.Static;
using FictionsDownload.UI.PopDialog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ListView;

namespace FictionsDownload.UI
{
    public partial class FormWebFormatList : Form
    {
        #region 内部变量和属性
        WebFormat webFormat = new WebFormat();
        #endregion
        #region 构造方法
        public FormWebFormatList()
        {
            InitializeComponent();
            InitializeComponentCode();
        }
        #endregion
        #region 控件初始化
        private void InitializeComponentCode()
        {
            lv_WebFormatListInit();
        }
        #endregion
        #region lv_WebFormatList控件相关
        private void lv_WebFormatListInit()
        {
            //初始化列
            lv_WebFormatList.View = View.Details;
            lv_WebFormatList.GridLines = true;
            lv_WebFormatList.FullRowSelect = true;
            //列头
            lv_WebFormatList.Columns.Add("Id", 30, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("Name", 180, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("Url", 350, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("HtmlHead", 100, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("HtmlTail", 100, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("VolumeHead", 100, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("VolumeTail", 100, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("TextHead", 100, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("TextTail", 100, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("CreateTime", 100, HorizontalAlignment.Left);
            lv_WebFormatList.Columns.Add("ActiveTime", 100, HorizontalAlignment.Left);
            //加载内容
            lv_WebFormatListRefresh();
        }
        private void lv_WebFormatListRefresh()
        {
            List<WebFormatModel> webFormats = webFormat.GetList<WebFormatModel>();

            lv_WebFormatList.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度
            //清空数据
            lv_WebFormatList.Items.Clear();
            //添加数据
            foreach (var webFormat in webFormats)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = webFormat.Id.ToString();
                lvi.SubItems.Add(webFormat.Name);
                lvi.SubItems.Add(webFormat.Url);
                lvi.SubItems.Add(webFormat.HtmlHead);
                lvi.SubItems.Add(webFormat.HtmlTail);
                lvi.SubItems.Add(webFormat.VolumeHead);
                lvi.SubItems.Add(webFormat.VolumeTail);
                lvi.SubItems.Add(webFormat.TextHead);
                lvi.SubItems.Add(webFormat.TextTail);
                lvi.SubItems.Add(webFormat.CreateTime.ToString());
                lvi.SubItems.Add(webFormat.ActiveTime.ToString());
                lv_WebFormatList.Items.Add(lvi);
            }
            lv_WebFormatList.EndUpdate();  //结束数据处理，UI界面一次性绘制。
        }
        private void lv_WebFormatList_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                cms_WebFormatList.Show(this, new Point(e.X, e.Y)); 
            }
        }
        #endregion
        private void ShowFormWebFormatEdit(EditState editState,string id)
        {
            FormWebFormatEdit formWebFormatEdit = new FormWebFormatEdit(editState,id);
            formWebFormatEdit.ShowDialog();
            lv_WebFormatListRefresh();
        }
        private void btn_Add_MouseClick(object sender, MouseEventArgs e)
        {
            ShowFormWebFormatEdit(EditState.新建,null);
        }

        private void btn_Mod_MouseClick(object sender, MouseEventArgs e)
        {
            if (lv_WebFormatList.SelectedItems.Count > 0)
            {
                ShowFormWebFormatEdit(EditState.修改, lv_WebFormatList.SelectedItems[0].Text);
            }
        }

        private void btn_Del_MouseClick(object sender, MouseEventArgs e)
        {
            删除ToolStripMenuItem_Click(sender,e);
        }

        private void lv_WebFormatList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var lv = (ListView)sender;
            if(lv.SelectedItems.Count > 0)
            {
                ShowFormWebFormatEdit(EditState.修改, lv.SelectedItems[0].Text);
            }
        }


        private void btn_Accept_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowFormWebFormatEdit(EditState.新建, null);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lv_WebFormatList.SelectedItems.Count > 0)
            {
                ShowFormWebFormatEdit(EditState.修改, lv_WebFormatList.SelectedItems[0].Text);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectedListViewItemCollection selected = lv_WebFormatList.SelectedItems;
            if (selected.Count > 0)
            {
                string delNames = "";
                List<string> webFormatIds = new List<string>();
                for (int i = 0; i < selected.Count; i++)
                {
                    webFormatIds.Add(selected[i].SubItems[0].Text);
                    delNames += selected[i].SubItems[1].Text + ":" + selected[i].SubItems[2].Text + "\n";
                }
                DialogResult result = MessageBox.Show("是否删除:\n" + delNames, "删除", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    //这一个删除循环，是直接写在这里，还是将List传入BLL，在BLL中循环获取webFormat然后删除？
                    foreach(string itemID in webFormatIds)
                    {
                        var item = webFormat.Find<WebFormatModel>(itemID); //不知道这里有没有办法不指定WebFormat的方法，放WebFormat继承BaseBLL的时候就指定T的类型
                        webFormat.Delete(item);
                    }
                    webFormat.SaveChanges();
                    lv_WebFormatListRefresh();
                }
            }
        }
    }
}
